package com.heima.wemedia.interceptors;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.wemedia.utils.WmThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:篍
 * @Date: 2025/09/18 16:38
 * @Description:
 */
@Slf4j
@Component
public class WmTokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String wmUserIdStr = request.getHeader("wmUserId");
        if (StringUtils.isBlank(wmUserIdStr)){
            log.error("wmUserId is null");
            return false;
        }
        Integer wmUserId = Integer.valueOf(wmUserIdStr);
        log.info("wmUserId:{}",wmUserId);
        WmThreadLocalUtil.setWmUserId(wmUserId);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        WmThreadLocalUtil.removeWmUserId();
    }
}

